package com.huaxiafinance.management.controller;


import com.huaxiafinance.management.dto.request.QueryPayOrderDto;
import com.huaxiafinance.management.dto.request.SettlementManageDto;
import com.huaxiafinance.management.exception.BusinessException;
import com.huaxiafinance.management.service.SettlementManageService;
import com.huaxiafinance.management.util.MessageResult;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Controller("SettlementManageController")
@RequestMapping("/settlementManage")
public class SettlementManageController {

  private static final Logger logger = Logger.getLogger(SettlementManageController.class);
  @SuppressWarnings("SpringJavaAutowiringInspection")
  @Autowired
  private SettlementManageService settlementManageService;


  /***
   * 商户端修改积分商城订单
   * @param request
   * @param response
   * @return
   * @throws Exception
   * @auther liyunqi
   * 20170612
   */
  @RequestMapping(value = "/updateOrderInfoSettlement", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String updateOrderInfo(@Valid @ModelAttribute("dto") SettlementManageDto dto,
      HttpServletRequest request, HttpServletResponse response) throws Exception {
    String responseMsg = null;

    MessageResult messageResult = new MessageResult();
    try {
      logger.info("商户端修改积分商城订单====================>");

      settlementManageService.updateOrderInfoSettlement(dto);

      messageResult.setSuccessResult();

    } catch (BusinessException be) {
      logger.info("商户端修改积分商城订单异常====================");
      messageResult.setFailureResult(be);
    } catch (Exception e) {
      logger.info("商户端修改积分商城订单异常====================");
      messageResult.setFailureResult();
    }
    logger.info("商户端修改积分商城订单结束====================");
    return messageResult.getAsJSON();
  }

  @RequestMapping(value = "/query", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String query(@Valid @ModelAttribute("dto") QueryPayOrderDto dto) {
    logger.info("查询支付订单列表请求的参数========================>" + dto.toString());
    MessageResult result = settlementManageService.getPayOrderList(dto);
    logger.info("查询支付订单列表响应的数据========================>" + result.getAsJSON());
    return result.getAsJSON();

  }

  @RequestMapping(value = "/download", produces = "application/json; charset=utf-8")
  public void download(@Valid @ModelAttribute("dto") QueryPayOrderDto dto,
      HttpServletResponse response) {
    logger.info("下载支付订单列表请求的参数========================>" + dto.toString());
    try {
      settlementManageService.download(dto, response);

    } catch (Exception ex) {
      logger.info("下载支付订单列表响应的出错========================>" + ex.toString());
    }

  }
}
